# _*_ coding: utf-8 _*_
__author__ = 'xiazi'
__date__ = '2017/6/28 13:38'


from .models import Course, Lesson, Video, CourseResource, BannerCourse
import xadmin


class LessonInline(object):
    # 显示对应表的信息
    model = Lesson
    extra = 0


class CourseResourceInline(object):
    model = CourseResource
    extra = 0


class CourseAdmin(object):
    list_display = ['name', 'desc', 'detail', 'is_banner', 'degree', 'learn_time', 'students', 'fav_nums',
                    'image', 'click_nums', 'add_time', 'get_zj_nums', 'go_to']
    search_fields = ['name', 'desc', 'detail', 'is_banner', 'degree', 'learn_time', 'students', 'fav_nums',
                     'image', 'click_nums']
    list_filter = ['name', 'desc', 'detail', 'is_banner', 'degree', 'learn_time', 'students', 'fav_nums',
                   'image', 'click_nums', 'add_time']
    # 后台中的课程按 点击数 排序
    ordering = ['-click_nums']
    # 把字段设置为只读，字段的值不可修改
    readonly_fields = ['students', 'click_nums']
    # 隐藏字段 后台不可见
    exclude = ['fav_nums']
    # 设置后台每隔几秒的时间刷新一次后台页面
    refresh_times = [3, 5]
    # 把对应表的信息引入该表中，在后台添加修改本表的信息时 可一起添加修改引入的表
    inlines = [LessonInline, CourseResourceInline]
    # 富文本编辑器
    style_fields = {"detail": "ueditor"}

    def queryset(self):
        # 对表的信息进行过滤
        qs = super(CourseAdmin, self).queryset()
        qs = qs.filter(is_banner=False)
        return qs

    def save_models(self):
        # 在保存课程的时候统计课程机构的课程数
        obj = self.new_obj
        obj.save()
        if obj.course_org is not None:
            course_org = obj.course_org
            course_org.course_nums = Course.objects.filter(course_org=course_org).count()
            course_org.save()


class BannerCourseAdmin(object):
    list_display = ['name', 'desc', 'detail', 'is_banner', 'degree', 'learn_time', 'students', 'fav_nums',
                    'image', 'click_nums', 'add_time', 'get_zj_nums', 'go_to']
    search_fields = ['name', 'desc', 'detail', 'is_banner', 'degree', 'learn_time', 'students', 'fav_nums',
                     'image', 'click_nums']
    list_filter = ['name', 'desc', 'detail', 'is_banner', 'degree', 'learn_time', 'students', 'fav_nums',
                   'image', 'click_nums', 'add_time']
    # 后台中的课程按 点击数 排序
    ordering = ['-click_nums']
    # 把字段设置为只读，字段的值不可修改
    readonly_fields = ['students', 'click_nums']
    # 隐藏字段 后台不可见
    exclude = ['fav_nums']
    # 在信息的列表页可以更改的字段
    list_editable = ['name', 'desc', 'degree']
    # 把对应表的信息引入该表中，在后台添加修改本表的信息时 可一起添加修改引入的表
    inlines = [LessonInline, CourseResourceInline]
    # 富文本编辑器
    style_fields = {"detail": "ueditor"}

    def queryset(self):
        # 对表的信息进行过滤
        qs = super(BannerCourseAdmin, self).queryset()
        qs = qs.filter(is_banner=True)
        return qs


class LessonAdmin(object):
    list_display = ['course', 'name', 'add_time']
    search_fields = ['course', 'name']
    list_filter = ['course', 'name', 'add_time']


class VideoAdmin(object):
    list_display = ['lesson', 'name', 'add_time']
    search_fields = ['lesson', 'name', 'add_time']
    list_filter = ['lesson', 'name', 'add_time']


class CourseResourceAdmin(object):
    list_display = ['course', 'name', 'download', 'add_time']
    search_fields = ['course', 'name', 'download']
    list_filter = ['course', 'name', 'download', 'add_time']


xadmin.site.register(Course, CourseAdmin)
xadmin.site.register(BannerCourse, BannerCourseAdmin)
xadmin.site.register(Lesson, LessonAdmin)
xadmin.site.register(Video, VideoAdmin)
xadmin.site.register(CourseResource, CourseResourceAdmin)